package com.gardeneaters.game_v0.playingfield;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;

// there will be different kinds of mushroom groups which will compete for power 
class MushroomGroup {

	private int dx,dy;
	private int x,y,width,height;
	private Context2d context;
	private Canvas mushroomImage;
	private int type;
	//private int groupNum; // group number tells which group/team it belongs to
	
	private Mushroom [] mushrooms;
	private int count=0;
	
	protected MushroomGroup(Context2d _context, int _x, int _y, int _width, int _height, int _team){
		this.context = _context;
		this.type = _team;
	  	
	  	mushrooms = new Mushroom[6];
	  	
	  	mushrooms[0] = new Mushroom(context, 0.05, 0.08, 0.95, 0.77);//TODO
	  	mushrooms[1] = new Mushroom(context, 0.03,0.50,0.25,0.25);
	  	mushrooms[2] = new Mushroom(context, 0.24,0.60,0.25,0.25);
	  	mushrooms[3] = new Mushroom(context, 0.44,0.69,0.25,0.25);
	  	mushrooms[4] = new Mushroom(context, 0.64,0.60,0.25,0.25);
	  	mushrooms[5] = new Mushroom(context, 0.78,0.50,0.25,0.25);
	  	
	  	resize(_x,_y,_width,_height);
	}
	
	protected void resize(int _x, int _y, int _width, int _height){
		//redraw Mushroom on canvas and save it for draw method to call again and again
		this.x = _x;
		this.y = _y;
		this.width = _width;
		this.height = _height;
		//Draw.resize();// now a new canvas will be created with new size.. so when this.getImage() is called its gd
		getImage();
		
		for(int k=0; k<mushrooms.length; k++)
		{
			mushrooms[k].resize(x,y,width,height,mushroomImage);
		}
	}
	
	private void getImage(){
		switch(type){
			case 1:
			{
				mushroomImage = Draw.redMushroom(width, height);
				break;
			}
			case 2:
			{
				mushroomImage = Draw.blueMushroom(width, height);
				break;
			}
			case 3:
			{
				mushroomImage = Draw.purpleMushroom(width, height);
				break;
			}
		
		}
	}

	protected void draw(double power){
		
		for(int i=0; i<mushrooms.length; i++)
		{
			if(Double.compare(power,1.0)==1)
			{
				mushrooms[i].draw(1.0);
				power -= 1.0;
			}
			else
			{
				mushrooms[i].draw(power);
				power=0;
			}
		}
		
//		for(int j =0; j<count; j++)// ensures previous mushrooms are full grown
//		{
//			mushrooms[j].draw(0.0);
//		}
//		if(count<6)
//			if(mushrooms[count].draw(0.0))
//				count++;
	}
	
	protected void setType(int _type){
		// when type i.e team changes this method will be called
		this.type = _type;
		getImage();
		for(int k=0; k<mushrooms.length; k++)
		{
			mushrooms[k].setImage(mushroomImage);
		}
		this.count=0;
	}
}
